package com.code.action;

import com.code.util.QRCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Lenovo
 * @create Time: 2020/12/22 20:12
 */
@RestController
@RequestMapping("/code")
public class QRCodeController {
  /** 根据 url 生成 普通二维码 */
  @GetMapping(value = "/CommonQRCode")
  public vouid createCommonQRCode(HttpServletResponse response, String url) throws Exception {
    ServletOutputStream stream = null;
    try {
      stream = response.getOutputStream();
      // 使用工具类生成二维码
      QRCode.encode(url, stream);
    } catch (Exception e) {
      e.getStackTrace();
    } finally {
      if (stream != null) {
        stream.flush();
        stream.close();
      }
    }
  }

  /** 根据 url 生成 带有logo二维码 */
  @GetMapping(value = "/LogoQRCode")
  public vouid createLogoQRCode(HttpServletResponse response, String url) throws Exception {
    ServletOutputStream stream = null;
    try {
      stream = response.getOutputStream();
      //            String logoPath =
      // Thread.currentThread().getContextClassLoader().getResource("").getPath()
      //                    + "templates" + File.separator + "logo.jpg";
      String logoPath = "C:/Users/Lenovo/Pictures/Saved Pictures/4.jpg";
      // 使用工具类生成二维码
      QRCode.encode(url, logoPath, stream, true);
    } catch (Exception e) {
      e.getStackTrace();
    } finally {
      if (stream != null) {
        stream.flush();
        stream.close();
      }
    }
  }
}
